# coding = utf-8

'''
灰度共生矩阵---提出来一个点，根据改点进行寻找
'''

import cv2,os
import numpy as np
import matplotlib.pyplot as plt
import SimpleITK as sitk
from PIL import Image
import math

def specilization(image):
    image = image*255
    image = image.astype(np.int64)
    image = image / 8
    image = image.astype(np.uint8)
    return image


def glgc(image, a, b, instace_level=32):


    '''
    min_data = np.min(image)
    max_data = np.max(image)

    if max_data != min_data and (max_data-min_data) > instace_level - 1:
            image = (image-min_data) / (max_data-min_data)
            image = image*(instace_level-1)
            image = image.astype(np.uint8)
    else:
            image = image - min_data
    '''



    glgc_matrix = np.zeros((instace_level, instace_level))

    for i in range(image.shape[0]):
        for j in range(image.shape[1]):
            f1 = image[i][j]
            if i+a < 0 or i+a >= image.shape[0] or j + b < 0  or j + b >= image.shape[1]:
                continue
            else:
                f2 = image[i+a][j+b]
            glgc_matrix[f1][f2] += 1

    return glgc_matrix


def get_glgc_map(image, kernel, a, b):
    entropy_map = np.zeros((image.shape[0], image.shape[1]))
    energy_map = np.zeros((image.shape[0], image.shape[1]))
    contrast_map = np.zeros((image.shape[0],image.shape[1]))
    differ_moment = np.zeros((image.shape[0], image.shape[1]))

    supply_matrix = np.zeros((32, 32))
    for i in range(supply_matrix.shape[0]):
        for j in range(supply_matrix.shape[1]):
            supply_matrix[i][j] = math.pow((i-j), 2)

    for i in range(0+kernel//2, image.shape[0]-kernel//2):
        for j in range(0+kernel//2, image.shape[1]-kernel//2):
            matrix1 = glgc(image[i - kernel // 2: i + kernel // 2 + 1, j - kernel // 2: j + kernel // 2 + 1], a=1, b=0)
            matrix1 = matrix1 / matrix1.sum()
            matrix2 = glgc(image[i - kernel // 2: i + kernel // 2 + 1, j - kernel // 2: j + kernel // 2 + 1], a=-1, b=0)
            matrix2 = matrix2 / matrix2.sum()
            matrix3 = glgc(image[i - kernel // 2: i + kernel // 2 + 1, j - kernel // 2: j + kernel // 2 + 1], a=0, b=1)
            matrix3 = matrix3 / matrix3.sum()
            matrix4 = glgc(image[i - kernel // 2: i + kernel // 2 + 1, j - kernel // 2: j + kernel // 2 + 1], a=0, b=-1)
            matrix4 = matrix4 / matrix4.sum()

            matrix = (matrix1 + matrix2 + matrix3 + matrix4) / 4
            entropy_map[i,j] = (-matrix * np.log(matrix+0.0000001)).sum()
            energy_map[i,j] = np.power(matrix, 2).sum()
            contrast_map[i,j] = (supply_matrix*matrix).sum()
            differ_moment[i, j] = ((1/(1+supply_matrix)) * matrix).sum()


    return (entropy_map, energy_map, contrast_map, differ_moment)

def show(image,fusion_image):
    plt.subplot(2, 3, 1)
    spec = specilization(image)
    plt.title("spec image")
    plt.imshow(spec, cmap="gray")
    (entropy_map0, energy_map, contrast_map, differ_moment) = get_glgc_map(image=spec, kernel=3, a=1, b=0)
    entropy_map0[entropy_map0 < 0] = 0
    plt.subplot(2, 3, 2)
    plt.title("entropy")
    plt.imshow(entropy_map0, cmap="gray")
    plt.subplot(2, 3, 3)
    plt.title("energy")
    plt.imshow(1-energy_map, cmap="gray")
    plt.subplot(2, 3, 4)
    plt.title("contrast")
    plt.imshow(contrast_map, cmap="gray")
    plt.subplot(2, 3, 5)
    plt.title("differ_moment")
    plt.imshow(differ_moment, cmap="gray")
    plt.subplot(2,3,6)
    plt.title("fusion")
    plt.imshow(fusion_image, cmap="gray")
    plt.show()


def get_vector(image, point, kernel, a, b):
    supply_matrix = np.zeros((32, 32))
    for i in range(supply_matrix.shape[0]):
        for j in range(supply_matrix.shape[1]):
            supply_matrix[i][j] = math.pow((i - j), 2)

    (y,x) = point
    matrix1 = glgc(image[x - kernel // 2: x + kernel // 2 + 1, y - kernel // 2: y + kernel // 2 + 1], a=a, b=b)
    matrix = matrix1 / matrix1.sum()
    entropy = (-matrix * np.log(matrix + 0.0000001)).sum()
    energy = np.power(matrix, 2).sum()
    contrast = (supply_matrix * matrix).sum()
    differ_moment = ((1 / (1 + supply_matrix)) * matrix).sum()

    print(entropy, energy, contrast, differ_moment)





def read_data():
    def find_data(case_id, origion_id, index):
         big_image = "E:\Dataset\Liver\qiye\DongBeiDaXue2\image_venous\\data2_{}_venous.mha".format(origion_id)
         big_liver = "E:\Dataset\Liver\qiye\DongBeiDaXue2\liver\\data2_{}_liver_label.mha".format(origion_id)
         big_tumor = "E:\Dataset\Liver\qiye\DongBeiDaXue2\lesion\\data2_{}_lesion_label.mha".format(origion_id)
         big_fusion = "E:\predict\image_tumor\case_{}\\fusion\\{}.png".format(str(case_id).zfill(5), str(index).zfill(3))
         big_image = sitk.GetArrayFromImage(sitk.ReadImage(big_image))
         big_image[big_image<=-200] = -200
         big_image[big_image > 250] = 250
         big_image = (big_image+200) / 450
         big_image = big_image[index]
         big_liver = sitk.GetArrayFromImage(sitk.ReadImage(big_liver))
         big_liver = big_liver[index]
         big_tumor = sitk.GetArrayFromImage(sitk.ReadImage(big_tumor))
         big_tumor = big_tumor[index]
         big_fusion = Image.open(big_fusion)
         return (big_image, big_fusion, big_liver, big_tumor)

    (big_image, big_fusion, big_liver, big_tumor) = find_data(case_id=74, origion_id="0656", index=40)
    (middle_image, middle_fusion, middle_liver, middle_tumor) = find_data(case_id=67, origion_id="0415", index=135)
    (small_image, small_fusion, small_liver, small_tumor) = find_data(case_id=79, origion_id="0865", index=262)
    (little_image, little_fusion, little_liver, little_tumor) = find_data(case_id=72, origion_id="0628", index=142)


    spec = specilization(big_image)
    get_vector(image=spec, kernel=7, point=(107,279), a=1, b=0)
    get_vector(image=spec, kernel=7, point=(107,279), a=-1, b=0)
    get_vector(image=spec, kernel=7, point=(107,279), a=0, b=1)
    get_vector(image=spec, kernel=7, point=(107,279), a=0, b=-1)
    get_vector(image=spec, kernel=7, point=(107,279), a=1, b=1)
    get_vector(image=spec, kernel=7, point=(107,279), a=-1, b=-1)
    #get_vector(image=spec, kernel=7, point=(114,239), a=1, b=0)
    #get_vector(image=spec, kernel=7, point=(174,198), a=1, b=0)
    #get_vector(image=spec, kernel=7, point=(157,199), a=1, b=0)
    #get_vector(image=spec, kernel=7, point=(369,246), a=1, b=0)
    #get_vector(image=spec, kernel=7, point=(367,70), a=1, b=0)

    plt.subplot(1, 2, 1)
    plt.imshow(big_fusion)
    plt.subplot(1, 2, 2)
    plt.imshow(spec, cmap="gray")
    plt.show()









if __name__ == '__main__':
    read_data()
